package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.qixi.UserQixiData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

public class UserQixiDaoImpl extends TempDataObjectDao<UserQixiData> {
    public static String getUserQixiDataSql = "select * from `t_u_qixi` where `activityId`=? and `userId`=?";
    public UserQixiData getUserQixiData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserQixiDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserQixiDataSql = "update `t_u_qixi` set getFlowerCount=?,sendFlowerCount=?,dayCount1=?,dayCount2=?,dayCount3=?,lastGetTime=?,getBroadcastList=?,watchBroadcastList=?,lastWatchTime=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(UserQixiData userQixiData) {
        userQixiData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userQixiData.getGetFlowerCount()));
        para.put(i++, new DbParameter(Types.INTEGER, userQixiData.getSendFlowerCount()));
        para.put(i++, new DbParameter(Types.INTEGER, userQixiData.getDayCount1()));
        para.put(i++, new DbParameter(Types.INTEGER, userQixiData.getDayCount2()));
        para.put(i++, new DbParameter(Types.INTEGER, userQixiData.getDayCount3()));
        para.put(i++, new DbParameter(Types.BIGINT, userQixiData.getLastGetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userQixiData.getGetBroadcastList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userQixiData.getWatchBroadcastList())));
        para.put(i++, new DbParameter(Types.BIGINT, userQixiData.getLastWatchTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userQixiData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userQixiData.getUserId()));
        if (getDb().executeNoneQuery(updateUserQixiDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserQixiDataSql = "insert into `t_u_qixi`(activityId,userId,getFlowerCount,sendFlowerCount,dayCount1,dayCount2,dayCount3,lastGetTime,getBroadcastList,watchBroadcastList,lastWatchTime) values (?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserQixiData userQixiData) {
        userQixiData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userQixiData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userQixiData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userQixiData.getGetFlowerCount()));
        para.put(i++, new DbParameter(Types.INTEGER, userQixiData.getSendFlowerCount()));
        para.put(i++, new DbParameter(Types.INTEGER, userQixiData.getDayCount1()));
        para.put(i++, new DbParameter(Types.INTEGER, userQixiData.getDayCount2()));
        para.put(i++, new DbParameter(Types.INTEGER, userQixiData.getDayCount3()));
        para.put(i++, new DbParameter(Types.BIGINT, userQixiData.getLastGetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userQixiData.getGetBroadcastList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userQixiData.getWatchBroadcastList())));
        para.put(i++, new DbParameter(Types.BIGINT, userQixiData.getLastWatchTime()));
        if (getDb().executeNoneQuery(insertUserQixiDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserQixiData parseData(ResultSet rs) throws SQLException {
        UserQixiData userQixiData = new UserQixiData();
        userQixiData.setActivityId(rs.getInt("activityId"));
        userQixiData.setUserId(rs.getLong("userId"));
        userQixiData.setGetFlowerCount(rs.getInt("getFlowerCount"));
        userQixiData.setSendFlowerCount(rs.getInt("sendFlowerCount"));
        userQixiData.setDayCount1(rs.getInt("dayCount1"));
        userQixiData.setDayCount2(rs.getInt("dayCount2"));
        userQixiData.setDayCount3(rs.getInt("dayCount3"));
        userQixiData.setLastGetTime(rs.getLong("lastGetTime"));
        userQixiData.setGetBroadcastList(JSON.parseObject(rs.getString("getBroadcastList"), new TypeReference<HashSet<Long>>(){}));
        userQixiData.setWatchBroadcastList(JSON.parseObject(rs.getString("watchBroadcastList"), new TypeReference<HashSet<Long>>(){}));
        userQixiData.setLastWatchTime(rs.getLong("lastWatchTime"));

        return userQixiData;
    }
}
